package com.cat.model.biz.service.admin.impl;

import java.io.File;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alonew.core.commons.tuple.TwoTuple;
import com.alonew.db.dao.TxCommonDAO;
import com.cat.model.biz.service.admin.InterviewService;
import com.cat.model.common.model.common.InterviewVO;
import com.cat.model.common.util.FileUtil;
import com.cat.model.common.util.StaticFileUtil;

@Service("interviewService")
public class InterviewServiceImpl implements InterviewService{

	@Autowired
	private TxCommonDAO commonDAO;
	
	@Override
	public InterviewVO createInterview(InterviewVO interviewVO,File coverImage,String coverImageFileName) {
		if(coverImage != null){
			String filePath = StaticFileUtil.appApkRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(coverImage);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + filePath, coverImageFileName, md5AndData.second);
			interviewVO.setCoverImage(filePath + "/" + coverImageFileName);
		}
		interviewVO.setCreateTime(new Date());
		interviewVO.setStatus(0);
		commonDAO.insert(interviewVO);
		
		return interviewVO;
	}

	@Override
	public int deleteInterview(long id) {
		InterviewVO interviewVO = new InterviewVO();
		interviewVO.setId(id);
		return commonDAO.deleteById(interviewVO);
	}

	@Override
	public int updateInterview(InterviewVO interviewVO,File coverImage,String coverImageFileName) {
		if(coverImage != null){
			String filePath = StaticFileUtil.appApkRelativePath() + FileUtil.buildDatePath();
			TwoTuple<String, byte[]> md5AndData = FileUtil.readAndCalMd5(coverImage);
			FileUtil.writeFile(StaticFileUtil.staticRootPath() + filePath, coverImageFileName, md5AndData.second);
			interviewVO.setCoverImage(filePath + "/" + coverImageFileName);
		}
		interviewVO.setStatus(0);
		return commonDAO.updateById(interviewVO, "name","type", "url","introduction","coverImage","ordering","status");
	}

}
